<?php
namespace app\job;

use app\common\model\Agent;
use app\common\model\AgentMoneyRecord;
use app\common\model\AgentWithdrawalRecord;
use app\common\model\PlatformMoneyRecord;
use think\Db;
use think\Exception;
use think\Log;
use think\Queue;
use think\queue\Job;
use Wxcrypt\XcxPay;

class WxPay extends BaseJob
{
    /**
     * 商家转账到零钱定时更新状态
     * @param Job $job
     * @param $data
     */
    public function handle(Job $job, $data)
    {
        Db::startTrans();//开启事务
        try{
            // 有些消息在到达消费者时,可能已经不再需要执行了
            $info = AgentWithdrawalRecord::get(['id'=>$data['id']]);
            if (!empty($info)){
                if ($info['status'] == 4){
                    $xccPay = new XcxPay();
                    $result = $xccPay->detailsQuery($info['out_batch_no'],$info['out_batch_no']);
                    if (isset($result['code']))
                        throw new Exception('申请微信支付失败：'.$result['message']);
                    if (isset($result['fail_reason'])){//转账失败
                        $agent = Agent::get($info->agent_id);
                        //退回余额
                        $balance = bcadd($agent->money,$info->money,2);
                        $agent->money = $balance;
                        $agent->save();
                        //退回记录
                        AgentMoneyRecord::create(['type'=>4,'money'=>$info->money,'balance'=>$balance,'agent_id'=>$agent->id]);
                        $info->status = 3;//设为失败
                        $info->detail_status = $result['detail_status'];//记录状态
                        $info->fail_reason = $result['fail_reason'];//记录失败原因
                        $info->save();
                        Queue::push(\app\job\Message::class,[
                            'member_id'=>$agent['member_id'],
                            'template_id'=>config('wechat_gzh')['gzh_template']['tixian_audit'],
                            'data'=>[
                                'time1'=>date('Y-m-d'),
                                'amount2'=>$info->money,
                                'thing3'=>'提现审核失败'
                            ]
                        ],'message');
                    }else{//转账成功
                        $agent = Agent::get($info->agent_id);
                        $info->status = 2;
                        $info->save();
                        //平台金额记录
                        PlatformMoneyRecord::create(['type'=>2,'detail_type'=>PlatformMoneyRecord::DETAIL_TYPE_AGENT_TX,'money'=>$info['money'],'remark'=>$info['id'],'createtime'=>time()]);
                        if (!empty($agent) && !empty($agent['member_id'])){
                            Queue::push(\app\job\Message::class,[
                                'member_id'=>$agent['member_id'],
                                'template_id'=>config('wechat_gzh')['gzh_template']['tixian_success'],
                                'data'=>[
                                    'time3'=>$info['createtime'],
                                    'character_string2'=>$info['id'],
                                    'thing4'=>'微信零钱' ,
                                    'amount5'=>$info['money'],
                                    'thing7'=>"本人"
                                ]
                            ],'message');
                        }
                    }
                }
            }
            Db::commit();

        }catch (Exception $e){
            Db::rollback();
            Log::error($e->getMessage());
            throw $e;
        }
    }









}